//package jee.boot.module.oauth2.interceptor;
//
//import jee.boot.common.utils.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
//import org.springframework.security.oauth2.common.OAuth2AccessToken;
//import org.springframework.security.oauth2.provider.ClientDetails;
//import org.springframework.security.oauth2.provider.ClientDetailsService;
//import org.springframework.security.oauth2.provider.OAuth2Authentication;
//import org.springframework.security.oauth2.provider.token.TokenStore;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Date;
//
///**
// * 自动刷新token过期时间
// */
//@Component
//public class AutoRefreshTokenInterceptor implements HandlerInterceptor {
//
//	private Logger logger = LoggerFactory.getLogger(AutoRefreshTokenInterceptor.class);
//
//	@Autowired
//	private TokenStore tokenStore;
//	@Autowired
//	private ClientDetailsService clientDetailsService;
//	@Value("${oauth.enableAutoRefreshToken:true}")
//	private  boolean enableAutoRefreshToken=true;
//	@Override
//	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//		String path = request.getRequestURI();
//		// 跳过静态资源,静态资源不会刷新超时时间
//		if(StringUtils.startsWith(path,"/static/")){
//			return true;
//		}
//		if(enableAutoRefreshToken) {
//			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//			if (authentication != null) {
//				if (authentication instanceof OAuth2Authentication) {
//					OAuth2Authentication auth = (OAuth2Authentication) authentication;
//					OAuth2AccessToken accessToken = tokenStore.getAccessToken(auth);
//					if (accessToken instanceof DefaultOAuth2AccessToken) {
//						DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
//						ClientDetails client = clientDetailsService.loadClientByClientId(auth.getOAuth2Request().getClientId());
//						Integer validity = client.getAccessTokenValiditySeconds();
//						token.setExpiration(new Date(System.currentTimeMillis() + validity * 1000));
//						tokenStore.storeAccessToken(token, auth);
//						logger.info("刷新session超时时间");
//					}
//				}
//
//			}
//		}
//		return true;
//	}
//
//	@Override
//	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//	}
//
//	@Override
//	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//
//	}
//
//
//}
